<template>
  <el-container class="contanier">
    <!-- 头部区域 -->
    <el-header>
      <div>
        <img src="../assets/hehe.jpg" alt="" />
        <span>电商管理平台</span>
      </div>
      <el-button @click="logout">退出</el-button>
    </el-header>

    <!-- 主体区域 -->
    <el-container>
      <!-- 侧边栏 -->
      <el-aside :width="isCollapse? '64px': '200px'">
        <div class="toggin-button" @click="togginCollapse">|||</div>
        <el-menu
          background-color="#333744"
          text-color="#fff"
          active-text-color="#409eff"
          unique-opened
          :collapse='isCollapse'
          :collapse-transition='false'
          router
          default-active= '/users'
        >
        <!-- 一级导航栏 -->
          <el-submenu :index="item.id + ''" v-for="item in MenuList" :key="item.id">
            <template slot="title">
              <i :class="iconsObj[item.id]"></i>
              <span>{{item.authName}}</span>
            </template>
            <!-- 二级导航栏 -->
            <el-menu-item
            :index="'/' + item2.path"
            v-for="item2 in item.children"
            :key="item2.id"
            @click="saveNavState('/' + item2.path)">
              <i class="el-icon-menu"></i>
              <span>{{item2.authName}}</span>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <!-- 主体内容区域 -->
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  data () {
    return {
      MenuList: [],
      // 一级导航栏的图标
      iconsObj: {
        125: 'iconfont icon-users',
        103: 'iconfont icon-showpassword',
        101: 'iconfont icon-shangpin',
        102: 'iconfont icon-danju',
        145: 'iconfont icon-baobiao'
      },
      // 是否折叠
      isCollapse: false,
      // 被激活的链接地址
      activePath: ''
    }
  },
  created () {
    // 页面一加载就获取侧边栏数据
    this.getMenuList()
  },
  methods: {
    // 退出
    logout () {
      window.sessionStorage.clear()
      this.$router.push('/login')
    },
    // 获取侧边栏数据的函数
    async getMenuList () {
      const { data: res } = await this.$http.get('menus')
      if (res.meta.status !== 200) return this.$messasge('请求失败')
      this.MenuList = res.data
    },
    // 折叠栏的处理函数
    togginCollapse () {
      this.isCollapse = !this.isCollapse
    },
    // 是否高亮的处理函数
    saveNavState (activePath) {
      window.sessionStorage.setItem('activePath', activePath)
      this.activePath = activePath
    }
  }
}
</script>

<style lang="less" scoped>
.el-header {
  background-color: #373d41;
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding-left: 0;
  > div {
    display: flex;
    align-items: center;
  }
  img {
    height: 50px;
    width: 50px;
    margin-right: 20px;
  }
}
.el-aside {
  background-color: #333744;
}
.el-main {
  background-color: #eaedf1;
}
.contanier {
  height: 100%;
  width: 100%;
}
.iconfont{
  padding-right: 10px;
}
.el-menu{
  border-right: none;
}
.toggin-button{
  text-align: center;
  font-size: 12px;
  color: #eaedf1;
  background-color: #4a5a64;
  line-height: 24px;
  letter-spacing: 0.2em;
  cursor: pointer;
}
</style>
